
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/Template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>CircleU</title>
<?php
 	session_start(); 
	if(!isset($_GET["CID"])){
		header('Location: error_page.php');
		//echo "No CID!!!";
	}
	if( !$_SESSION["login"])header('Location: error_page.php');
	include("db_util.php");
	putenv("ORACLE_HOME=/usr/local/libexec/oracle/app/oracle/product/11.2.0/client_1");
	$connection=DbConnect();
	if(!$connection)
		echo "DisConnected!<br>";
	if(isset($_GET["GSID"]))$SID=$_GET["GSID"]; else $SID=$_SESSION["StuID"];
	
		// Yan Qiao
	$cid = $_GET["CID"];
	$course = get_course_info_with_con($connection, $cid);
	if(isset($_POST["add_course"])){ // add course
		add_course($connection, $_SESSION["StuID"], $cid);
		//echo "Course Added.";
	}else if(isset($_POST["quit_course"])){
		quit_course($connection, $_SESSION["StuID"], $cid);
	}//~Yan Qiao
	
	$query='SELECT * FROM Student where sid=' . $SID; 
	$statement = oci_parse($connection, $query);
	$query='SELECT name, GID FROM STUGROUP SG, GROUPS G WHERE sg.STUDENTID=' . $SID . 'and G.GID=SG.GroupID';
	$statement1 = oci_parse($connection, $query);
	$query='SELECT cname, CID FROM StuCourse SC, Course C WHERE sc.STUDENTID=' . $SID . 'and C.CID=SC.CourseID';
	$statement2 = oci_parse($connection, $query);
	oci_execute($statement);
	oci_execute($statement1);
	oci_execute($statement2);
	ocifetch ( $statement ) ;
?>
<!-- InstanceEndEditable -->
<style type="text/css">
<!--

a:link   {
text-decoration:   none;
}
a:visited   {
text-decoration:   none;
}
a:hover   {
text-decoration:   underline;
} 
.try {
	background-color: #FFFFFF;
	position: relative;
	z-index: auto;
	visibility: visible;
	clip: rect(auto,auto,auto,auto);
	width: auto;
	height: 76px;
}
.copyright {
	position: absolute;
	left: 150px;
	top: 600px;
	font-size: large;
	height: 22px;
}
.LayerImg {
	left: auto;
}
.CircleU {
	font-family: "Comic Sans MS";
	font-size: xx-large;
	font-weight: bold;
	position: relative;
	left: 20px;
	top: auto;
	height: auto;
	width: auto;
	visibility: visible;
	background-position: left;
	color: #3333FF;
}

#Title1 {
	position:absolute;
	width:1096px;
	height:60px;
	z-index:2;
	left: 9px;
	top: 6px;
}



}

#Dwt01 {
	position:absolute;
	width:1092px;
	height:500px;
	right: 9px;
	top: 80px;
}
#LeftLayer {
	position:absolute;
	width:200px;
	height:500px;
	z-index:3;
	left: 11px;
	top: 77px;
	background-color: #FF6600;
}
#EditR1 {
	position:absolute;
	width:878px;
	height:500px;
	z-index:4;
	left: 221px;
	top: 77px;
}
.STYLE3 {color: #0000CC}
-->
</style><script type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
//-->
</script>
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>

<body>
<center>
  <div id="Title1">
    <table width="1092" height="59" bgcolor="#FF6600">
      <tr>
        <td rowspan="2" class="CircleU"><div align="left"><?php echo "<a href=\"index.php\">"; ?> <span class="STYLE3">CircleU</a></span></div></td>
        <td width="128"><div align="right"><?php echo "<a href=\"Personal.php\">"; ?> My Page </a></div></td>
        <td width="111"><div align="right"><?php echo "<a href=\"SignOut.php\">"; ?>Sign Out </a></div></td>
        <td width="120"><div align="center"><?php echo "<a href=\"ContactUs.php\">"; ?>Contact Us</a></div></td>
      </tr>
      <tr>
        <td><form id="form2" name="form2" method="post" <?php echo "action=\"AdvancedSearch.php\""; ?>>
          <label>
            <input type="submit" name="AdvancedSearch" value="Advanced Search" />
          </label>
        </form>
        </td>
        <td colspan="2"><form id="form1" name="form1" method="post" <?php echo "action=\"SearchResult.php\""; ?>>
          <label>
            <input type="text" name="SearchText" />
          </label>
          <label>
          <input type="submit" name="SearchResult" value="search" />
          </label>
        </form>        </td>
      </tr>
    </table>
  </div>
  <div class="copyright">
<p>Copyright @ 2011 CircleU built in University of Florida. All right reserved&#13;</p>

</div>

 
  
  

  <div id="LeftLayer">
  <div id="Layerleft2">
  <?php

  if(isset($_GET["GSID"]))echo "<a href=PictureView.php?GSID=" . OCIResult( $statement, 'SID' ). "><img src=\"". OCIResult( $statement, 'PROFILE_PIC' ) . "\" width=\"200\" height=\"133\" /></a>";
  else echo "<a href=PictureView.php><img src=\"". OCIResult( $statement, 'PROFILE_PIC' ) . "\" width=\"200\" height=\"133\" /></a>";
 
  ?>
  </div>
  <div id="Layerleft3">
  <?php 
  
  
  function MostPopularCourse($connection)
	{
	
	$query='select courseid, cname, max(savg) from (select courseid, avg(score) as savg, count(*) as cnum from courseReview cr, CoursePost cp where cr.CPID=cp.CPID group by CourseID having count(*)>=2) res, course c where c.cid=res.courseid group by courseid, cname';
	
	$MostPopularCourse = oci_parse($connection, $query);
	oci_execute($MostPopularCourse);
	return $MostPopularCourse;
	}
  
  function AddAsFriend($connection)
	{
	$query='Insert into isfriend values(' . $_SESSION["StuID"] . ',' . $_GET["GSID"] . ')'; 
	$InsertRelationShip = oci_parse($connection, $query);
	oci_execute($InsertRelationShip);
	oci_free_statement($InsertRelationShip)	;
	$query='Insert into isfriend values(' . $_GET["GSID"] . ',' . $_SESSION["StuID"] . ')'; 
	$InsertRelationShip = oci_parse($connection, $query);
	oci_execute($InsertRelationShip);
	oci_free_statement($InsertRelationShip)	;
	}
	
	 function RemoveFriend($connection)
	{
	$query='Delete from isfriend where (s1=' . $_SESSION["StuID"] . ' and s2=' . $_GET["GSID"] . ') or (s1=' . $_GET["GSID"] . 'and s2=' . $_SESSION["StuID"] . ')'; 
	$DeleteRelationShip = oci_parse($connection, $query);
	oci_execute($DeleteRelationShip);
	
	oci_free_statement($DeleteRelationShip)	;
	}
	
	if(isset($_POST["Remove"]))RemoveFriend($connection);
	else
	{
	if(isset($_POST["Add"]))AddAsFriend($connection);
	}
	
  
  if(!isset($_GET["GSID"])) echo "<a href=\"ProfileSetting.php\">Profile Setting</a>"; else 
 {
 	$query='SELECT * FROM ISFRIEND where (s1=' . $_SESSION["StuID"] . ' and s2=' . $_GET["GSID"] . ') or (s1=' . $_GET["GSID"] . ' and s2=' . $_SESSION["StuID"] . ')'; 
	$checkIfFriend = oci_parse($connection, $query);
	oci_execute($checkIfFriend);
  echo "<form id=\"removeoradd\" name=\"removeoradd\" method=\"post\" action=\"\">" . "<input type=\"submit\" "; if(ocifetch($checkIfFriend)) echo "name=\"Remove\" value=\"Remove\""; else echo "name=\"Add\" value=\"Add as friend\"";
 oci_free_statement($checkIfFriend); echo "/></form>"; 
 } 
  ?>
    <table width="200" border="1" bgcolor="#FF6600" id="profile" style="table-layout: fixed;" >
	<tr>
        <td width="84"><span class="STYLE4">First Name: </span></td>
        <td width="100"><?php
  echo OCIResult( $statement, 'FIRST_NAME' );
  ?></td>
      </tr>
	  <tr>
        <td width="84"><span class="STYLE4">Last Name: </span></td>
        <td width="100"><?php
  echo OCIResult( $statement, 'LAST_NAME' );
  ?></td>
      </tr>
      <tr>
        <td width="84"><span class="STYLE4">Department: </span></td>
        <td width="100"><?php
  echo OCIResult( $statement, 'DEPARTMENT' );
  ?></td>
      </tr>
      <tr>
        <td><span class="STYLE4">Birthday:</span></td>
        <td><?php
  echo OCIResult( $statement, 'BIRTHDAY' );
  ?></td>
      </tr>
      
      <tr>
        <td><span class="STYLE4">Hobbies:</span></td>
        <td><?php
  echo OCIResult( $statement, 'HOBBIES' );
  ?></td>
      </tr>
     <tr>
	 
        <td width="84"><span class="STYLE4"><?php 
	 if(isset($_GET["GSID"]))
	 echo "<a href=\"PictureView.php?GSID=" . $SID . "\"> Album </a></span>"; else  
	 echo "<a href=\"PictureView.php\"> Album </a></span>";
	 ?></td>
      
      </tr>
      <tr>
        <td style= "word-break:break-all;"><span class="profile">Groups:</span></td>
       <?php echo "<td style= \"word-break:break-all; width:40\">"; while(ocifetch($statement1)){echo "<a href=\"GroupPage.php?GID=" . OCIResult( $statement1, 'GID' ) . "\">" .
 OCIResult( $statement1, 'NAME' ) . ";</a>";} 
	
  ?></td>  </tr>
   <tr>
        <td><span class="profile">Courses:</span></td>
       <?php echo "<td>"; while(ocifetch($statement2)){echo "<a href=\"CourseHome.php?CID=" . OCIResult( $statement2, 'CID' ) . "\">" .
 OCIResult( $statement2, 'CID' ) . ";</a>";} 
  ?></td>      </tr>
 
  
  <?php 
  if(!isset($_GET["GSID"])){
  echo "<tr>
        
		<td width=\"84\"><span class=\"STYLE4\"><a href=\"FriendList.php\"> Friends </a></span> </td>
      
      </tr>";
  }
  ?>
   <tr>
        <td></td>
       <td></td>      </tr>
  
   <tr>
        <td><span class="profile">MVP Courses:</span></td>
       <?php echo "<td>"; $temp=MostPopularCourse($connection);ocifetch($temp); echo "<a href=\"CourseHome.php?CID=" . OCIResult( $temp, 'COURSEID' ) . "\">" .
 OCIResult( $temp, 'COURSEID' ) . ";</a>"; oci_free_statement($statement);oci_free_statement($statement1);oci_free_statement($statement2);
 oci_free_statement($temp);
	oci_close($connection);
  ?></td>      </tr>
  
     
    </table>
  </div>
  
  </div>
  <div id="EditR1"><!-- InstanceBeginEditable name="EditRegion3" -->
  <div id="Layer3">
	<?php
		$con = DbConnect();
	?>
	<h2>
   		<?php echo $course["CID"] . ": " . $course["CNAME"]; echo " (" . $course["SEMESTER"] .") "; ?>
	</h2>
	<h3><?php echo "Instructor: " . $course["INSTRUCTOR"]; ?></h3>
	<form  id="form2" name="form2" method="post" action="CourseHome.php?CID=<?php echo $course["CID"]; ?>"> 
	 
	<?php  // show average score
	  $query = "select count(*) as n_reviews, avg(Score) as avg_score from CoursePost cp, CourseReview cr 
	  			where cp.CPID=cr.CPID and cp.CourseID=". $course["CID"];
	  $stmt = oci_parse($con, $query);
	  oci_execute($stmt);
	  $scores = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
	  oci_free_statement($stmt);
	  
	  echo "Average Score<b>:" . $scores["AVG_SCORE"] . "</b> (" . $scores["N_REVIEWS"] . " reviews) ";
	  if(is_selected_course($con, $_SESSION["StuID"], $cid))
			echo "<input type=\"submit\" name=\"quit_course\" value=\"Withdraw This Course\"/>";
		else echo "<input type=\"submit\" name=\"add_course\" value=\"Add to My Course\"/>";
	?>
	</form>
    <table width="850" border="0">
      <tr>
        <td width= "150" style="text-align:right">Time: </td>
        <td style="text-align:left"><?php echo $course["LECTURE_TIME"]; ?></td>
		<td style="text-align:right">Location: </td>
        <td><?php echo $course["LOCATION"]; ?></td>
      </tr>
	  <tr>
        <td style="text-align:right">Textbook: </td>
        <td colspan="3"><?php echo $course["TEXTBOOK"]; ?></td>
      </tr>
      <tr>
        <td style="text-align:right">TAs: </td>
        <td colspan="3"><?php echo $course["TA"]; ?></td>
      </tr>
      <tr>
        <td style="text-align:right">Office Hours: </td>
        <td colspan="3"><?php echo $course["OFFICE_HOURS"]; ?></td>
      </tr>
      <tr>
        <td style="text-align:right" valign="top">Description: </td>
        <td colspan="3"><?php echo $course["DESCRIPTION"]; ?></td>
      </tr>
	  <tr>
	  	<td colspan="4"><hr /></td>
	  </tr>
    </table>
  </div>
    <div id="Layer4"> 
	<form id="form1" name="form1" method="post" action="CreateCoursePost.php?CID=<?php echo $cid;?>">
    <table width="850" border="0">
	<?php
		// fetch all pages from DB
		$posts = get_posts_with_con($con, $cid);
		
		// deciding how many pages in total and which page to show.
		$max_post_per_page = 3;
		$num_of_posts = count($posts);
		$max_page = ceil($num_of_posts / $max_post_per_page);
		if(!isset($_GET["post_page"])||$_GET["post_page"]<1||$_GET["post_page"]>$max_page)
			$post_page=1;
		else
			$post_page=$_GET["post_page"];
		//----------------
	?>
      <tr>
        <td>Latest Post: </td>
		<td>&nbsp;</td>
		<td style="text-align:right">
		<?php
			// Printing "next page", "previous page" buttons.
			if($post_page==1) 	echo "<< "; 
			else echo "<a href=\"CourseHome.php?CID=" . $cid . "&post_page=" . ($post_page-1). "\"><<</a> ";
			if($post_page==$max_page)	echo ">> ";
			else	echo"<a href=\"CourseHome.php?CID=" . $cid . "&post_page=" . ($post_page+1). "\">>></a> ";
			
			// new post button.
			if(is_selected_course($con, $_SESSION["StuID"], $cid))
				echo "<input type=\"submit\" name=\"new_post\" value=\"New Post\" />";
			else
				echo "<input type=\"button\" value=\"New Post\" onclick=\"alert('Please add course before posting a message.')\" />";
		?></td>
      </tr>
	  <?php
	  //-------printing posts cutting into pages
		for($i=($max_post_per_page*($post_page-1)); $i<($max_post_per_page*$post_page) && $i<$num_of_posts; $i++){
			echo "<tr>\n";
			echo "<td style=\"text-align:left\"><a href='Personal.php?SID=" . $posts[$i]["AUTHOR"] ."'>" . $posts[$i]["FIRST_NAME"] . "</a> said: </td>";
   			echo "<td style=\"text-align:left\"><a href=\"CoursePost.php?CID=" . $cid . "&CPID=". $posts[$i]["CPID"] . "\">". $posts[$i]["SUBJECT"] . "</a></td>";
			echo "<td style=\"text-align:right\"><a href=\"CoursePost.php?CID=" . $cid . "&CPID=". $posts[$i]["CPID"] . "\">" . $posts[$i]["POST_TIME"] . "</a></td>";
   			echo "</tr>\n";
			echo "<tr>\n";
			echo "<td> &nbsp; </td>";
			echo "<td colspan='2'>" . substr($posts[$i]["CONTENT"], 0, 80) . " ...<a href=CoursePost.php?CPID=". $posts[$i]["CPID"] . ">reply</a></td>";
			echo "</tr>\n";
		}		
		//------
		oci_close($con);
	  ?>
	  <tr><td colspan="3"><hr /></td></tr>
    </table>
    </form>
    <p>&nbsp;</p>
  </div>
  <!-- InstanceEndEditable --></div>
</center>
</body>
<!-- InstanceEnd --></html>
